package com.jctrip.hotel.route.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.jctrip.hotel.common.util.OssUtil;
import com.jctrip.hotel.domain.dao.HtHotelStdImageDao;
import com.jctrip.hotel.domain.entity.HtHotelStdImage;
import com.jctrip.hotel.domain.entity.HtHotelStdInfo;
import com.jctrip.hotel.domain.entity.HtPricePolicy;
import com.jctrip.hotel.route.model.param.GetHotelPictureParam;
import com.jctrip.hotel.route.model.result.GetHotelPictureResult;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 获取酒店图片接口-服务
 */
@Service
public class GetHotelPictureService extends RouteService {
    @Autowired
    private HtHotelStdImageDao hotelStdImageDao;

    public GetHotelPictureResult execute(GetHotelPictureParam param) {
        // 价格政策配置
//        List<HtPricePolicy> htPricePolicies = getPricePolicies();
        List<HtPricePolicy> htPricePolicies = getPricePolicies();
        List<String> sourceCodes = htPricePolicies.stream()
                .map(HtPricePolicy::getSourceCode)
                .collect(Collectors.toList());
        // 酒店标准信息
        HtHotelStdInfo htHotelStdInfo = getHotelStdInfo(param.getHotelId(), sourceCodes);
        if (htHotelStdInfo == null) {
            throw new RuntimeException("无效的hotelId");
        }
        List<HtHotelStdImage> htHotelStdImages =
                Optional.ofNullable(hotelStdImageDao.selectList(
                        new LambdaQueryWrapper<HtHotelStdImage>()
                                .eq(HtHotelStdImage::getHotelId, param.getHotelId())))
                        .orElse(new ArrayList<>());
        String hotelImage = htHotelStdImages.stream()
                .filter(e -> e.getImageType() == 1
                        && StringUtils.isNotBlank(e.getSourceUrl()))
                .findFirst().orElse(new HtHotelStdImage()).getSourceUrl();

        List<String> hotelImages = htHotelStdImages.stream()
                .filter(e -> e.getImageType() == 2
                        && StringUtils.isNotBlank(e.getSourceUrl()))
                .map(HtHotelStdImage::getSourceUrl)
                .collect(Collectors.toList());

        List<GetHotelPictureResult.RoomUrl> hotelRoomTypeImages = htHotelStdImages.stream()
                .filter(e -> e.getImageType() == 3
                        && StringUtils.isNotBlank(e.getSourceUrl()))
                .map(htHotelStdImage -> GetHotelPictureResult.RoomUrl.builder()
                        .roomTypeCode(htHotelStdImage.getRoomTypeCode())
                        .url(htHotelStdImage.getSourceUrl())
                        .build())
                .sorted(Comparator.comparing(GetHotelPictureResult.RoomUrl::getRoomTypeCode))
                .collect(Collectors.toList());

        return GetHotelPictureResult.builder()
                .hotelImage(hotelImage)
                .hotelImages(hotelImages)
                .hotelRoomTypeImages(hotelRoomTypeImages)
                .build();
    }
}
